package com.pp.demo.module.user.controller;

import com.pp.demo.common.response.Result;
import com.pp.demo.module.user.converter.UserConverter;
import com.pp.demo.module.user.dto.UserDTO;
import com.pp.demo.module.user.param.UserCreateParam;
import com.pp.demo.module.user.param.UserUpdateParam;
import com.pp.demo.module.user.service.UserService;
import com.pp.demo.module.user.vo.UserVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pzh
 * @since 2025-06-11 21:29:42
 */
@Api(tags = "用户管理")
@Slf4j
//@Validated
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/listUser")
    public Result<List<UserVO>> listUser() {
        List<UserDTO> dtos = userService.listUser();
        List<UserVO> vos = UserConverter.INSTANCE.toVOList(dtos);
        return Result.success(vos);
    }

    @GetMapping("/getUser")
    public Result<UserVO> getUser(@RequestParam String name) {
        UserDTO dto = userService.getUser(name);
        UserVO vo = UserConverter.INSTANCE.toVO(dto);
        return Result.success(vo);
    }

    @PostMapping("/createUser")
    public Result<Long> createUser(@RequestBody @Valid UserCreateParam param) {
        Long userId = userService.createUser(param);
        return Result.success(userId);
    }

    @PutMapping("/updateUser/{id}")
    public Result<Boolean> updateUser(@PathVariable Long id, @RequestBody @Valid UserUpdateParam param) {
        boolean flag = userService.updateUser(id, param);
        return Result.success(flag);
    }

    @DeleteMapping("/removeUser/{id}")
    public Result<Boolean> removeUser(@PathVariable Long id) {
        boolean flag = userService.removeUser(id);
        return Result.success(flag);
    }
}
